﻿using Sidvall.Serialization;
using System;
using System.Globalization;
using System.Windows.Data;

namespace Sidvall.WorkItemManager.UI.Wpf.Converters
{
	public class StringToTimeSpanConverter : IValueConverter
	{
		#region Public Members

		#region Convert

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return TimeSpanToString(value);
		}

		#endregion
		#region ConvertBack

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return StringToTimeSpan(value);
		}

		#endregion

		#endregion
		#region Private Members

		#region StringToTimeSpan

		private object StringToTimeSpan(object value)
		{
			if (value is string)
			{
				return value.ToString(string.Empty).ToNullableTimeSpan();
			}
			return null;
		}

		#endregion
		#region TimeSpanToString

		private object TimeSpanToString(object value)
		{
			if (value is System.TimeSpan)
			{
				return ((System.TimeSpan)value).ToString();
			}
			return null;
		}

		#endregion

		#endregion
	}
}
